package com.erebus.config;


import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
public class MqConfirmConfig implements ApplicationContextAware {

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        RabbitTemplate rabbitTemplate = applicationContext.getBean(RabbitTemplate.class);

        // 配置消息返回回调（旧版方式）
        rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> {
            log.debug("收到消息的return callback，exchange:{}, key:{}, msg:{}, code:{}, text:{}",
                    exchange, routingKey, message, replyCode, replyText);
        });

        // 配置消息确认回调（ConfirmCallback）
        rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
            if (ack) {
                System.out.println("消息已成功发送到交换机，消息ID：" + correlationData.getId());
            } else {
                System.err.println("消息发送失败，原因：" + cause + "，消息ID：" + correlationData.getId());
            }
        });
    }
}